package cn.iocoder.yudao.module.dbit.controller.admin.jobinfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dbit.controller.admin.jobinfo.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.jobinfo.JobInfoDO;
import cn.iocoder.yudao.module.dbit.service.jobinfo.JobInfoService;

import javax.servlet.http.HttpServletResponse;

@Tag(name = "管理后台 - 企业职位信息")
@RestController
@RequestMapping("/dbit/job-info")
@Validated
public class JobInfoController {

    private static final Logger log = LoggerFactory.getLogger(JobInfoController.class);
    @Resource
    private JobInfoService jobInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建企业职位信息")
    @PreAuthorize("@ss.hasPermission('dbit:job-info:create')")
    public CommonResult<Integer> createJobInfo(@Valid @RequestBody JobInfoSaveReqVO createReqVO) {
        return success(jobInfoService.createJobInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新企业职位信息")
    @PreAuthorize("@ss.hasPermission('dbit:job-info:update')")
    public CommonResult<Boolean> updateJobInfo(@Valid @RequestBody JobInfoSaveReqVO updateReqVO) {
        jobInfoService.updateJobInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除企业职位信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dbit:job-info:delete')")
    public CommonResult<Boolean> deleteJobInfo(@RequestParam("id") Integer id) {
        jobInfoService.deleteJobInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得企业职位信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dbit:job-info:query')")
    public CommonResult<JobInfoRespVO> getJobInfo(@RequestParam("id") Integer id) {
        JobInfoDO jobInfo = jobInfoService.getJobInfo(id);
        log.info("jobInfo:{}", BeanUtils.toBean(jobInfo, JobInfoRespVO.class));
        return success(BeanUtils.toBean(jobInfo, JobInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得企业职位信息分页")
    @PreAuthorize("@ss.hasPermission('dbit:job-info:query')")
    public CommonResult<PageResult<JobInfoRespVO>> getJobInfoPage(@Valid JobInfoPageReqVO pageReqVO) {
        PageResult<JobInfoRespVO> pageResult = jobInfoService.getJobInfoPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出企业职位信息 Excel")
    @PreAuthorize("@ss.hasPermission('dbit:job-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJobInfoExcel(@Valid JobInfoPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JobInfoRespVO> list = jobInfoService.getJobInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "企业职位信息.xls", "数据", JobInfoRespVO.class,
                BeanUtils.toBean(list, JobInfoRespVO.class));
    }

}